
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第八章 根据 Table 生成 Model · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="7migrate.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="1.写在前面的话.html">
            
                <a href="1.写在前面的话.html">
            
                    
                    写在前面的话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="2.搭建环境.html">
            
                <a href="2.搭建环境.html">
            
                    
                    第一章 搭建环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    第二章 模型定义
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="3.sequelize 实例变量.html">
            
                <a href="3.sequelize 实例变量.html">
            
                    
                    2.1 实例变量方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="4.定义一个模型.html">
            
                <a href="4.定义一个模型.html">
            
                    
                    2.2 定义一个模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="5.模型字段类型.html">
            
                <a href="5.模型字段类型.html">
            
                    
                    2.3 模型字段类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="6.导入与配置.html">
            
                <a href="6.导入与配置.html">
            
                    
                    2.3 导入与配置
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="3模型关系.html">
            
                <a href="3模型关系.html">
            
                    
                    第三章 模型关系
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="3.1属于.html">
            
                <a href="3.1属于.html">
            
                    
                    3.1 属于
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="3.2包含.html">
            
                <a href="3.2包含.html">
            
                    
                    3.2 包含
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="3.3一对多.html">
            
                <a href="3.3一对多.html">
            
                    
                    3.3 一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="3.4多对多.html">
            
                <a href="3.4多对多.html">
            
                    
                    3.4 多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="3.5多态一对多.html">
            
                <a href="3.5多态一对多.html">
            
                    
                    3.5 多态一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="3.6多态多对多.html">
            
                <a href="3.6多态多对多.html">
            
                    
                    3.6 多态多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="3.7关系嵌套.html">
            
                <a href="3.7关系嵌套.html">
            
                    
                    3.7 关系嵌套
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="4模型查询.html">
            
                <a href="4模型查询.html">
            
                    
                    第四章 模型查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="5查询范围.html">
            
                <a href="5查询范围.html">
            
                    
                    第五章 查询范围 Scope (预定义查询条件)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="6生命周期函数.html">
            
                <a href="6生命周期函数.html">
            
                    
                    第六章 生命周期函数 Hook
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="7migrate.html">
            
                <a href="7migrate.html">
            
                    
                    第七章 数据库迁移 Migrate
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.10" data-path="8tabel2model.html">
            
                <a href="8tabel2model.html">
            
                    
                    第八章 根据 Table 生成 Model
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第八章 根据 Table 生成 Model</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6839;&#x636E;-table-&#x751F;&#x6210;-model">&#x6839;&#x636E; Table &#x751F;&#x6210; Model</h1>
<p>&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x9700;&#x6C42;&#x5176;&#x5B9E;&#x975E;&#x5E38;&#x7684;&#x7B80;&#x5355;&#xFF0C;&#x56E0;&#x4E3A;&#x5B98;&#x65B9;&#x5DF2;&#x7ECF;&#x63D0;&#x4F9B;&#x7ED9;&#x4E86;&#x6211;&#x4EEC;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x4F7F;&#x7528; <code>sequelize-auto</code></p>
<p>&#x8FD9;&#x4E2A;&#x63D2;&#x4EF6;&#x7684;&#x5730;&#x5740; github repo <code>https://github.com/sequelize/sequelize-auto</code></p>
<pre><code>npm install -g sequelize-auto mysql
</code></pre><p>&#x4E4B;&#x6240;&#x4EE5;&#x5B89;&#x88C5; mysql &#x800C;&#x4E0D;&#x662F; mysql2 &#x662F;&#x56E0;&#x4E3A;&#x8FD9;&#x4E2A;&#x63D2;&#x4EF6;&#x91CC;&#x9762;&#x7684; sequelize &#x8FD8;&#x662F;3.x &#x7248;&#x672C;&#x3002;</p>
<p>&#x4E4B;&#x540E;&#x8FD0;&#x884C;</p>
<pre><code>sequelize-auto -o &quot;./models&quot; -d nodelover -h 127.0.0.1 -u root -p 3306 -x &apos;&apos; -e mysql
</code></pre><p>-d &#x662F;&#x6570;&#x636E;&#x5E93;&#x540D;&#x79F0;
-e &#x662F;&#x6570;&#x636E;&#x5E93;&#x7C7B;&#x522B;
-x &#x662F;&#x5BC6;&#x7801;
-o &#x662F;&#x8F93;&#x51FA;&#x5730;&#x5740;</p>
<p>&#x76EE;&#x524D;&#x6211;&#x5F97;&#x5230;&#x4EE5;&#x4E0B;&#x8F93;&#x51FA;</p>
<pre><code class="lang-ts">Executing (<span class="hljs-keyword">default</span>): SHOW TABLES;
Executing (<span class="hljs-keyword">default</span>): SELECT         K.CONSTRAINT_NAME as constraint_name       , K.CONSTRAINT_SCHEMA as source_schema       , K.TABLE_SCHEMA as source_table       , K.COLUMN_NAME as source_column       , K.REFERENCED_TABLE_SCHEMA AS target_schema       , K.REFERENCED_TABLE_NAME AS target_table       , K.REFERENCED_COLUMN_NAME AS target_column       , C.extra       , C.COLUMN_KEY AS column_key       FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K       LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS C         ON C.TABLE_NAME = K.TABLE_NAME AND C.COLUMN_NAME = K.COLUMN_NAME       WHERE         K.TABLE_NAME = <span class="hljs-string">&apos;Books&apos;</span>         AND K.CONSTRAINT_SCHEMA = <span class="hljs-string">&apos;nodelover&apos;</span>;
Executing (<span class="hljs-keyword">default</span>): SELECT         K.CONSTRAINT_NAME as constraint_name       , K.CONSTRAINT_SCHEMA as source_schema       , K.TABLE_SCHEMA as source_table       , K.COLUMN_NAME as source_column       , K.REFERENCED_TABLE_SCHEMA AS target_schema       , K.REFERENCED_TABLE_NAME AS target_table       , K.REFERENCED_COLUMN_NAME AS target_column       , C.extra       , C.COLUMN_KEY AS column_key       FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K       LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS C         ON C.TABLE_NAME = K.TABLE_NAME AND C.COLUMN_NAME = K.COLUMN_NAME       WHERE         K.TABLE_NAME = <span class="hljs-string">&apos;Comments&apos;</span>         AND K.CONSTRAINT_SCHEMA = <span class="hljs-string">&apos;nodelover&apos;</span>;
Executing (<span class="hljs-keyword">default</span>): SELECT         K.CONSTRAINT_NAME as constraint_name       , K.CONSTRAINT_SCHEMA as source_schema       , K.TABLE_SCHEMA as source_table       , K.COLUMN_NAME as source_column       , K.REFERENCED_TABLE_SCHEMA AS target_schema       , K.REFERENCED_TABLE_NAME AS target_table       , K.REFERENCED_COLUMN_NAME AS target_column       , C.extra       , C.COLUMN_KEY AS column_key       FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K       LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS C         ON C.TABLE_NAME = K.TABLE_NAME AND C.COLUMN_NAME = K.COLUMN_NAME       WHERE         K.TABLE_NAME = <span class="hljs-string">&apos;Images&apos;</span>         AND K.CONSTRAINT_SCHEMA = <span class="hljs-string">&apos;nodelover&apos;</span>;
Executing (<span class="hljs-keyword">default</span>): SELECT         K.CONSTRAINT_NAME as constraint_name       , K.CONSTRAINT_SCHEMA as source_schema       , K.TABLE_SCHEMA as source_table       , K.COLUMN_NAME as source_column       , K.REFERENCED_TABLE_SCHEMA AS target_schema       , K.REFERENCED_TABLE_NAME AS target_table       , K.REFERENCED_COLUMN_NAME AS target_column       , C.extra       , C.COLUMN_KEY AS column_key       FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K       LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS C         ON C.TABLE_NAME = K.TABLE_NAME AND C.COLUMN_NAME = K.COLUMN_NAME       WHERE         K.TABLE_NAME = <span class="hljs-string">&apos;Posts&apos;</span>         AND K.CONSTRAINT_SCHEMA = <span class="hljs-string">&apos;nodelover&apos;</span>;
Executing (<span class="hljs-keyword">default</span>): SELECT         K.CONSTRAINT_NAME as constraint_name       , K.CONSTRAINT_SCHEMA as source_schema       , K.TABLE_SCHEMA as source_table       , K.COLUMN_NAME as source_column       , K.REFERENCED_TABLE_SCHEMA AS target_schema       , K.REFERENCED_TABLE_NAME AS target_table       , K.REFERENCED_COLUMN_NAME AS target_column       , C.extra       , C.COLUMN_KEY AS column_key       FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K       LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS C         ON C.TABLE_NAME = K.TABLE_NAME AND C.COLUMN_NAME = K.COLUMN_NAME       WHERE         K.TABLE_NAME = <span class="hljs-string">&apos;Tags&apos;</span>         AND K.CONSTRAINT_SCHEMA = <span class="hljs-string">&apos;nodelover&apos;</span>;
Executing (<span class="hljs-keyword">default</span>): SELECT         K.CONSTRAINT_NAME as constraint_name       , K.CONSTRAINT_SCHEMA as source_schema       , K.TABLE_SCHEMA as source_table       , K.COLUMN_NAME as source_column       , K.REFERENCED_TABLE_SCHEMA AS target_schema       , K.REFERENCED_TABLE_NAME AS target_table       , K.REFERENCED_COLUMN_NAME AS target_column       , C.extra       , C.COLUMN_KEY AS column_key       FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K       LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS C         ON C.TABLE_NAME = K.TABLE_NAME AND C.COLUMN_NAME = K.COLUMN_NAME       WHERE         K.TABLE_NAME = <span class="hljs-string">&apos;Users&apos;</span>         AND K.CONSTRAINT_SCHEMA = <span class="hljs-string">&apos;nodelover&apos;</span>;
Executing (<span class="hljs-keyword">default</span>): SELECT         K.CONSTRAINT_NAME as constraint_name       , K.CONSTRAINT_SCHEMA as source_schema       , K.TABLE_SCHEMA as source_table       , K.COLUMN_NAME as source_column       , K.REFERENCED_TABLE_SCHEMA AS target_schema       , K.REFERENCED_TABLE_NAME AS target_table       , K.REFERENCED_COLUMN_NAME AS target_column       , C.extra       , C.COLUMN_KEY AS column_key       FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K       LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS C         ON C.TABLE_NAME = K.TABLE_NAME AND C.COLUMN_NAME = K.COLUMN_NAME       WHERE         K.TABLE_NAME = <span class="hljs-string">&apos;as&apos;</span>         AND K.CONSTRAINT_SCHEMA = <span class="hljs-string">&apos;nodelover&apos;</span>;
Executing (<span class="hljs-keyword">default</span>): SELECT         K.CONSTRAINT_NAME as constraint_name       , K.CONSTRAINT_SCHEMA as source_schema       , K.TABLE_SCHEMA as source_table       , K.COLUMN_NAME as source_column       , K.REFERENCED_TABLE_SCHEMA AS target_schema       , K.REFERENCED_TABLE_NAME AS target_table       , K.REFERENCED_COLUMN_NAME AS target_column       , C.extra       , C.COLUMN_KEY AS column_key       FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K       LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS C         ON C.TABLE_NAME = K.TABLE_NAME AND C.COLUMN_NAME = K.COLUMN_NAME       WHERE         K.TABLE_NAME = <span class="hljs-string">&apos;bs&apos;</span>         AND K.CONSTRAINT_SCHEMA = <span class="hljs-string">&apos;nodelover&apos;</span>;
Executing (<span class="hljs-keyword">default</span>): SELECT         K.CONSTRAINT_NAME as constraint_name       , K.CONSTRAINT_SCHEMA as source_schema       , K.TABLE_SCHEMA as source_table       , K.COLUMN_NAME as source_column       , K.REFERENCED_TABLE_SCHEMA AS target_schema       , K.REFERENCED_TABLE_NAME AS target_table       , K.REFERENCED_COLUMN_NAME AS target_column       , C.extra       , C.COLUMN_KEY AS column_key       FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K       LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS C         ON C.TABLE_NAME = K.TABLE_NAME AND C.COLUMN_NAME = K.COLUMN_NAME       WHERE         K.TABLE_NAME = <span class="hljs-string">&apos;user_tag&apos;</span>         AND K.CONSTRAINT_SCHEMA = <span class="hljs-string">&apos;nodelover&apos;</span>;
Executing (<span class="hljs-keyword">default</span>): DESCRIBE <span class="hljs-string">`Books`</span>;
Executing (<span class="hljs-keyword">default</span>): DESCRIBE <span class="hljs-string">`Comments`</span>;
Executing (<span class="hljs-keyword">default</span>): DESCRIBE <span class="hljs-string">`Images`</span>;
Executing (<span class="hljs-keyword">default</span>): DESCRIBE <span class="hljs-string">`Posts`</span>;
Executing (<span class="hljs-keyword">default</span>): DESCRIBE <span class="hljs-string">`Tags`</span>;
Executing (<span class="hljs-keyword">default</span>): DESCRIBE <span class="hljs-string">`Users`</span>;
Executing (<span class="hljs-keyword">default</span>): DESCRIBE <span class="hljs-string">`as`</span>;
Executing (<span class="hljs-keyword">default</span>): DESCRIBE <span class="hljs-string">`bs`</span>;
Executing (<span class="hljs-keyword">default</span>): DESCRIBE <span class="hljs-string">`user_tag`</span>;
Done!
</code></pre>
<p>&#x6253;&#x5F00;&#x6587;&#x4EF6;&#x4E5F;&#x770B;&#x5230;&#x4E86;&#x5BF9;&#x5E94;&#x7684;&#x6587;&#x4EF6;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="7migrate.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 第七章 数据库迁移 Migrate">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第八章 根据 Table 生成 Model","level":"1.10","depth":1,"previous":{"title":"第七章 数据库迁移 Migrate","level":"1.9","depth":1,"path":"document/7migrate.md","ref":"./document/7migrate.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"document/8tabel2model.md","mtime":"2017-06-28T06:53:06.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-06-28T08:29:37.466Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

